﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BoolanSlide.FileStruct;
using Windows.Storage;

namespace BoolanElement.Storage
{
    public class BooLibrary
    {

        public async static Task<BPTFileInformation> GetSingleThumbInformationFromLibrary(string filename)
        {
            StorageFolder library = await BooUtility.GetFolderFromDocumentFolderAsync(BooDocumentHelper.LibraryFolder);
            StorageFile item = await library.GetFileAsync(filename);

            BPTFileInformation info = await BPTFile.GetThumbInformation(item);

            info.FirstSlide = await BPTFile.GetFirstSlideFromFile(item);

            await BPTFile.GetImageResourceFromFile(info.FirstSlide, item);

            return info;

        }

        public async static Task<List<BPTFileInformation>> GetThumbInformationFromLibrary()
        {
            List<BPTFileInformation> list = new List<BPTFileInformation>();
            StorageFolder library = await BooUtility.GetFolderFromDocumentFolderAsync(BooDocumentHelper.LibraryFolder);
            IReadOnlyList<StorageFile> files = await library.GetFilesAsync();

            foreach (StorageFile item in files)
            {
                if (item.FileType == Configuration.GlobalVariable.FileExtension)
                {
                    BPTFileInformation info = await BPTFile.GetThumbInformation(item);
                    if (info == null)
                        continue;
                    info.FirstSlide = await BPTFile.GetFirstSlideFromFile(item);

                    if (info.FirstSlide == null)
                        continue;

                    await BPTFile.GetImageResourceFromFile(info.FirstSlide, item);
                    list.Add(info);
                }
            }
            return list;
           
        }

        public async static Task<List<BPTFileInformation>> GetThemeInformationFromLibrary()
        {
            List<BPTFileInformation> list = new List<BPTFileInformation>();
            Windows.ApplicationModel.Package package = Windows.ApplicationModel.Package.Current;
            Windows.Storage.StorageFolder installedLocation = package.InstalledLocation;
            StorageFolder file = await installedLocation.GetFolderAsync("Theme");

            IReadOnlyList<StorageFile> files = await file.GetFilesAsync();

            foreach (StorageFile item in files)
            {
                if (item.FileType == Configuration.GlobalVariable.FileExtension)
                {
                    BPTFileInformation info = await BPTFile.GetThumbInformation(item);
                    if (info == null)
                        continue;
                    info.FirstSlide = await BPTFile.GetFirstSlideFromFile(item);
                    await BPTFile.GetImageResourceFromFile(info.FirstSlide, item);
                    list.Add(info);
                }
            }
            return list;
        }

        public async static Task<BPTFileInformation> GetThemeInformationByGUID(String guid)
        {
            Windows.ApplicationModel.Package package = Windows.ApplicationModel.Package.Current;
            Windows.Storage.StorageFolder installedLocation = package.InstalledLocation;
            StorageFolder file = await installedLocation.GetFolderAsync("Theme");

            IReadOnlyList<StorageFile> files = await file.GetFilesAsync();

            foreach (StorageFile item in files)
            {
                if (item.FileType == Configuration.GlobalVariable.FileExtension)
                {
                    BPTFileInformation info=await BPTFile.GetThumbInformation(item);
                    if (info == null)
                        continue;
                    if (guid == info.StorageName)
                    {
                        return info;
                    }
                }
            }
            return null;
        }
    }
}
